from rest_framework.filters import OrderingFilter
from rest_framework.generics import CreateAPIView, RetrieveAPIView, UpdateAPIView, ListAPIView, GenericAPIView, \
    DestroyAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from users import serializers
from users.models import User, WriteArticle


class UserNameExist(APIView):
    """
    get: 注册判断用户名是否存在
    """

    def get(self, request):
        req_data = request.query_params
        username = req_data['username']
        count = User.objects.filter(username=username).count()
        data = {
            'count': count
        }
        return Response(data)


class UserMobileExist(APIView):
    """
    get: 注册判断手机号是否存在
    """

    def get(self, request):
        req_data = request.query_params
        mobile = req_data['mobile']
        count = User.objects.filter(mobile=mobile).count()
        data = {
            'count': count
        }
        return Response(data)


class UserSignCreate(CreateAPIView):
    """
    注册提交信息视图
    """
    serializer_class = serializers.UserSignCreateSerializer


class UserDetailView(RetrieveAPIView):
    """
    个人中心详情信息
    """
    serializer_class = serializers.UserDetailSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        a = self.request.user
        return self.request.user


class EditDetailsCreate(UpdateAPIView):
    """
    编辑资料提交保存视图
    """
    serializer_class = serializers.EditDetailsCreateSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        return self.request.user


class WriteArticleSave(CreateAPIView):
    """写文章保存"""
    serializer_class = serializers.WriteArticleSerializer
    permission_classes = [IsAuthenticated]


class UpdateTime(UpdateAPIView):
    """
    更新时间
    """
    serializer_class = serializers.UpdateTimeSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        return self.request.user


# 没有用ＤＲＦ框架的旧版本
# class MyArticleList(APIView):
#     """
#     获得文章列表
#     """
#
#     def get(self, request):
#         data1 = []
#         req_data = request.query_params
#         username = req_data['username']
#         users_list = User.objects.filter(username=username)
#         for i in users_list:
#             user_id = i.id
#             article_list = WriteArticle.objects.all().filter(users_id=user_id)
#             for y in article_list:
#                 update_time = str(i.update_time)[:-10]    # 时间的处理
#                 data = {
#                     'article_name': y.article_name,
#                     'article_classify': y.article_classify,
#                     'article_text': y.article_text,
#                     'update_time': update_time,
#                     'article_id': y.id,
#
#                 }
#                 data1.append(data)
#
#         return Response(data1)


class MyArticleList(ListAPIView):
    """
    我的文章 获得文章列表
    """
    serializer_class = serializers.MyArticleListSerializer
    permission_classes = [IsAuthenticated]
    pagination_class = None


    def get_queryset(self):
        user_id = self.kwargs['user_id']
        return WriteArticle.objects.filter(users_id_id=user_id)


class MyArticleDetail(ListAPIView):
    """
    获得文章详情
    """
    serializer_class = serializers.MyArticleDetailSerializer
    pagination_class = None

    def get_queryset(self):
        article_id = self.kwargs['article_id']
        return WriteArticle.objects.filter(id=article_id)


class ArticleDelete(DestroyAPIView):
    """
    删除文章
    """
    serializer_class = serializers.ArticleDeleteSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        article_id = self.kwargs['pk']
        return WriteArticle.objects.filter(id=article_id)




class GetArticle(ListAPIView):
    """
    首页获得文章
    """
    serializer_class = serializers.GetArticleSerializer
    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ('update_time',)


    def get_queryset(self):
        # article_id = self.kwargs['pk']
        return WriteArticle.objects.all()